package com.wangyi.im.remoting;

import com.wangyi.im.interceptor.BodyInterceptor;
import com.wangyi.im.interceptor.HeaderInterceptor;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.fastjson.FastJsonConverterFactory;

public abstract class Remoting {

    private String appKey;

    private String appSecret;

    private Retrofit.Builder builder;

    public Remoting(String baseUrl, String appKey, String appSecret){
        this.appKey = appKey;
        this.appSecret = appSecret;
        this.builder = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(FastJsonConverterFactory.create());
                //.addConverterFactory(GsonConverterFactory.create());
    }

    protected Retrofit getRetrofit(Object body) {
        return builder.client(new OkHttpClient.Builder()
                .addInterceptor(new HeaderInterceptor(appKey, appSecret))
                .addInterceptor(new BodyInterceptor(body))

                //此拦截器只能放最后否则无法打印请求header、 body 的参数
                .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                .build())
              .build();
    }

    public String getAppSecret(){
        return appSecret;
    }
}
